<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>分类页</title>
    <link rel="stylesheet" href="./css/style.css">
</head>

<body>
    <div class="fix-title">
        <div class="back">&lt;</div>
        <input type="text" placeholder="请输入关键字" class="input">
        <div class="text">确定</div>
    </div>
    <div class="history-list">
        <h3>历史记录 <span>全部删除</span></h3>
        <ul>
      
        </ul>
    </div>
    <div class="good-list">
        <ul>

        </ul>
        <div class="empty">
            <span>空空如也...</span>
        </div>
        <p class="desc">数据正在加载中...</p>
        <p class="desc">我也是有底线的...</p>
    </div>
</body>
<script src="../jquery.js"></script>
<script>
     let kk = []
    $('.text').on('click', function () {
        let k = $(".input").val().trim()
        kk.push(k)
        // 渲染记录
       getKk()
        console.log(kk);
        getXuan(k)
        $(".input").val('')
    })
    $('.back').on('click', function () {
        getChong()
    })
$('.history-list').on('click','span',function(){
   kk=[]
   getKk()
   getChong()
})
    function getXuan(k) {
        $.ajax({
            method: 'get',
            url: 'http://124.223.14.236:3001/api/goods',
            success(res) {
                let h = []
                res.data.list.filter(item => {
                    if ((item.title.indexOf(k) != -1)) {
                        h.push(item)
                    }
                    item.title.indexOf(k)
                })

                // 渲染新数组
                let m = h.map(item => {
                    return `
                            <li>
                                <img src="${item.img}" alt="">
                                <h3>${item.title}</h3>
                                <p>
                                    <span>￥${item.price.toFixed(2)}</span>
                                    <span>+</span>
                                </p>
                            </li>
                `
                }).join('')
                $('.good-list ul').html(m)
            }
        })
    }
function getKk(){
    let kkk= kk.map(item => {
            return `
            <li>${item}</li>           
                `
        }).join('')
        $('.history-list ul').html(kkk)
}

    function getChong() {
        $.ajax({
            method: 'get',
            url: 'http://124.223.14.236:3001/api/goods',
            success(res) {
                console.log(res);
                let m = res.data.list.map(item => {
                    return `
                            <li>
                                <img src="${item.img}" alt="">
                                <h3>${item.title}</h3>
                                <p>
                                    <span>￥${item.price.toFixed(2)}</span>
                                    <span>+</span>
                                </p>
                            </li>
                `
                }).join('')
                $('.good-list ul').html(m)
            }
        })
    }
    getChong()
    // $.ajax({
    //     method: 'get',
    //     url: `http://124.223.14.236:3001/api/goods/?{id=202}`,
    //     success(res) {
    //         console.log(res);
    //     }
    // })
</script>

</html>